//
//  LCKeFuCenterViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/24.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCKeFuCenterViewController.h"
#import <MessageUI/MessageUI.h>
#import "LCMineService.h"

@interface LCKeFuCenterViewController ()<MFMailComposeViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UILabel *kfPhoneLb;

@property (weak, nonatomic) IBOutlet UILabel *kfEmailLb;

@property (weak, nonatomic) IBOutlet UILabel *kfWechatLb;


@end

@implementation LCKeFuCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self getContactInfo];
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getContactInfo {
    [LCMineService getContactInfoBlock:^(BOOL isSuccess, NSString *tel, NSString *email, NSString *wxchar, NSString *qqchar) {
        if (isSuccess) {
            self.kfPhoneLb.text = tel;
            self.kfEmailLb.text = email;
            self.kfWechatLb.text = wxchar;
        }
    }];
}

- (IBAction)kfPhoneBtnAction:(UIButton *)sender {
    if ([self.kfPhoneLb.text isEqualToString:@"--"] || [self.kfPhoneLb.text isEqualToString:@"暂无"] || !self.kfPhoneLb.text.length) {
        [self showErrorText:@"暂无客服电话"];
        return;
    } else {
        if ([NSString isMobileNumber:self.kfPhoneLb.text]) {
            [LCFunction lc_callPhoneNumber:self.kfPhoneLb.text];
        } else {
            UIPasteboard *pab = [UIPasteboard generalPasteboard];
            NSString*string = self.kfPhoneLb.text;
            [pab setString:string];
            if(pab == nil) {
                [self showErrorText:@"复制失败"];
            }else{
                [self showSuccessText:@"复制成功"];
            }
        }
    }
}

- (IBAction)kfEmailBtnAction:(UIButton *)sender {
    if ([self.kfPhoneLb.text isEqualToString:@"--"] || [self.kfPhoneLb.text isEqualToString:@"暂无"] || !self.kfPhoneLb.text.length) {
        [self showErrorText:@"暂无客服邮箱"];
        return;
    } else {
        if ([NSString isValidateEmail:self.kfEmailLb.text]) {
            if ([MFMailComposeViewController canSendMail]) {
                MFMailComposeViewController * mailVC = [[MFMailComposeViewController alloc] init];
                mailVC.mailComposeDelegate = self;
                
                [mailVC setSubject:@"乐创客服"];
                [mailVC setToRecipients:@[self.kfEmailLb.text]];
                [self presentViewController:mailVC animated:YES completion:nil];
            } else {
                UIPasteboard *pab = [UIPasteboard generalPasteboard];
                NSString*string = self.kfEmailLb.text;
                [pab setString:string];
                if(pab == nil) {
                    [self showErrorText:@"复制失败"];
                }else{
                    [self showSuccessText:@"复制成功"];
                }
            }
        } else {
            UIPasteboard *pab = [UIPasteboard generalPasteboard];
            NSString*string = self.kfEmailLb.text;
            [pab setString:string];
            if(pab == nil) {
                [self showErrorText:@"复制失败"];
            }else{
                [self showSuccessText:@"复制成功"];
            }
        }
    }
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    switch (result) {
        case MFMailComposeResultCancelled: {
            //取消发送
        } break;
        case MFMailComposeResultFailed: {
            //发送失败
        } break;
        case MFMailComposeResultSaved: {
            //保存草稿
        } break;
        case MFMailComposeResultSent: {
            //发送成功
        } break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)kfWechatBtnAction:(UIButton *)sender {
    if ([self.kfWechatLb.text isEqualToString:@"--"] ||
        [self.kfWechatLb.text isEqualToString:@"暂无"] ||
        !self.kfWechatLb.text.length) {
        [self showErrorText:@"暂无客服微信"];
    } else {
        UIPasteboard *pab = [UIPasteboard generalPasteboard];
        NSString*string = self.kfWechatLb.text;
        [pab setString:string];
        if(pab == nil) {
            [self showErrorText:@"复制失败"];
        }else{
            [self showSuccessText:@"复制成功"];
        }
    }
}



@end
